// 引入 用来发送请求的方法 一定要把路径补全
import { request } from "../../request/index.js"

Page({
  data: {
    //轮播图数组
    swiperList:[],  
    //导航 数组
    catesList:[],
    // 楼层数据
    floorList:[]
  },
  //页面开始加载 就能触发
  onLoad: function(options){
    // 1 发送异步请求数据 获取轮播图 ,优化手段可以通过es6的 promise来解决这个问题
    /* wx.request({
      url: 'https://api-hmugo-web.itheima.net/api/public/v1/home/swiperdata',
      success: (result) => {
        this.setData({
          swiperList:result.data.message
        }) 
      }
    }); */
    /* request({url:'https://api-hmugo-web.itheima.net/api/public/v1/home/swiperdata'}).then(result =>{
      this.setData({
        swiperList:result.data.message
      }) 
    }) */

    this.getSwiperList();
    this.getCatesList();
    this.getFloorList();
  },
  //获取轮播图数据
  getSwiperList(){
    request({url:'/home/swiperdata'}).then(result =>{
      result.forEach((v, i) => { 
        result[i].navigator_url = v.navigator_url.replace('main', 'goods_detail')
      })
      this.setData({
        swiperList:result
      }) 
    })
  },
  //获取 分类导航数据
  getCatesList(){
    request({url:'/home/catitems'}).then(result =>{
      this.setData({
        catesList:result
      }) 
    })
  },
  // 获取楼层数据
  getFloorList(){
    request({url:'/home/floordata'}).then(result =>{
      console.log(result[0].product_list[3].navigator_url);
      result.forEach((v, i) => {
        const res=result[i].product_list;
        console.log(res);
        res.forEach((r,j) => {
          console.log(res[j].navigator_url)
          res[j].navigator_url = r.navigator_url.replace('goods_list', 'goods_list/goods_list');
        })
      })
      this.setData({
        floorList:result
      }) 
    })
  },
});